package com.oa.om.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.err.BizException;
import com.mdp.core.service.BaseService;
import com.oa.om.entity.OmApplianceRequire;
import com.oa.om.mapper.OmApplianceRequireMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author maimeng-mdp code-gen
 * @since 2023年10月9日
 */
@Service
public class OmApplianceRequireService extends BaseService<OmApplianceRequireMapper, OmApplianceRequire> {
    static Logger logger = LoggerFactory.getLogger(OmApplianceRequireService.class);

    /**
     * 自定义查询，支持多表关联
     *
     * @param page 分页条件
     * @param ew   一定要，并且必须加@Param("ew")注解
     * @param ext  如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
     * @return
     */
    public List<Map<String, Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String, Object> ext) {
        return baseMapper.selectListMapByWhere(page, ew, ext);
    }

    @Autowired
    private OmApplianceService omApplianceService;

    /** 请在此类添加自定义函数 */
    /**
     * 流程审批过程中回调该接口，更新业务数据
     * 如果发起流程时上送了restUrl，则无论流程中是否配置了监听器都会在流程发生以下事件时推送数据过来
     * eventName: PROCESS_STARTED 流程启动完成 全局
     * PROCESS_COMPLETED 流程正常结束 全局
     * PROCESS_CANCELLED 流程删除 全局
     * create 人工任务启动
     * complete 人工任务完成
     * assignment 人工任务分配给了具体的人
     * delete 人工任务被删除
     * TASK_COMPLETED_FORM_DATA_UPDATE 人工任务提交完成后，智能表单数据更新
     * <p>
     * 其中 create/complete/assignment/delete事件是需要在模型中人工节点上配置了委托代理表达式 ${taskBizCallListener}才会推送过来。
     * 在人工任务节点上配置 任务监听器  建议事件为 complete,其它assignment/create/complete/delete也可以，一般建议在complete,委托代理表达式 ${taskBizCallListener}
     *
     * @param flowVars {flowBranchId,agree,procInstId,startUserid,assignee,actId,taskName,mainTitle,branchId,bizKey,commentMsg,eventName,modelKey} 等
     * @return 如果tips.isOk==false，将影响流程提交
     **/
    @Transactional
    public void processApprova(IPage page, QueryWrapper qw, Map<String, Object> flowVars) {
        String eventName = (String) flowVars.get("eventName");
        String agree = (String) flowVars.get("agree");
        String branchId = (String) flowVars.get("branchId");
        String omAppRequireId = (String) flowVars.get("omApplianceRequireId");//办公用品申请编号
        String bizKey = (String) flowVars.get("bizKey");
        if ("om_appliance_require_approva".equals(bizKey)) {
        } else {
            throw new BizException("不支持的业务,请上送业务编码【bizKey】参数");
        }

        /**
         * 由于没建立om_appliance_require_approva表，不需要更新节点信息
         * if("complete".equals(eventName)) {}
         */

        //监听办公用品管理员，create->待领取
        // PROCESS_COMPLETED->已领取
        if ("TASK_CREATED".equals(eventName)) {
            //办公用品管理员节点ID已写死为om_appliance_keeper
            if ("om_appliance_keeper".equals(flowVars.get("actId"))) {
                OmApplianceRequire oar = new OmApplianceRequire();
                oar.setId(omAppRequireId);
                oar.setStatus("2");//2为待领取状态
                this.updateSomeFieldByPk(oar);
            }
        } else {
            if ("PROCESS_STARTED".equals(eventName)) {
                Map<String, Object> bizQuery = new HashMap<>();
                bizQuery.put("id", omAppRequireId);
                if (StringUtils.isEmpty(omAppRequireId)) {
                    throw new BizException("请上送办公用品申请单编号-omApplianceRequireId");
                }
                if (StringUtils.isEmpty(branchId)) {
                    throw new BizException("请上送branchId");
                }
                List<Map<String, Object>> bizList = this.selectListMapByWhere(page, qw, bizQuery);
                if (bizList == null || bizList.size() == 0) {
                    throw new BizException("没有找到对应办公用品申请单,申请单编号为【" + omAppRequireId + "】");
                } else {
                    Map<String, Object> bizObject = bizList.get(0);
                    if ("1".equals(bizObject.get("bizFlowState"))) {
                        throw new BizException("该办公用品申请单正在审批中，不能再发起审批");
                    }
                }
                OmApplianceRequire oar = new OmApplianceRequire();
                oar.setId(omAppRequireId);
                oar.setStatus("1");//1为审批中状态
                oar.setBizFlowState("1");
                oar.setBizProcInstId((String) flowVars.get("procInstId"));
                this.updateSomeFieldByPk(oar);
            } else if ("PROCESS_COMPLETED".equals(eventName)) {
                OmApplianceRequire oar = new OmApplianceRequire(omAppRequireId);
                if ("1".equals(agree)) {
                    oar.setStatus("3");//已领取
                    oar.setBizFlowState("3");
                } else {//根据流程，此处应该不会出现
                    oar.setStatus("3");//未通过
                    oar.setBizFlowState("3");
                }
                this.updateSomeFieldByPk(oar);
            } else if ("PROCESS_CANCELLED".equals(eventName)) {
                OmApplianceRequire oar = new OmApplianceRequire(omAppRequireId);
                oar.setStatus("5");
                oar.setBizFlowState("4");
                this.updateSomeFieldByPk(oar);
            }
        }
    }

}

